﻿#region
/*******************************************************************************
** File Name:		    OrganizationService.cs
** Creator:		        chao.jiang 
** EMail:               chao.jiang 
** Create date:	        
** Latest Modifier:
** Latest Modify date:
** Description:		    
** Modify History:
**
** Code Review:
**
** Version number:       1.0.0
*********************************************************************************/
#endregion
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Phoenix.Service.Contracts;
using Phoenix.Service.Model;
using Phoenix.IBatisRepository;
using Phoenix.Respository;
using System.Data;
using Phoenix.Entity;

namespace Phoenix.Service
{
    public class OrganizationService : IOrganizationService
    {
		 IOrganization organization = new OrganizationRepository();
		 
		 #region  Auto Generation

         public void AddOrganization(OrganizationModel model)
         {
             EOrganization entity = ConvertModelToEntity(model);

             organization.AddOrganization(entity);
         }

		public void UpdateByOrgID (OrganizationModel model)
		{
			EOrganization entity = ConvertModelToEntity(model);

            organization.UpdateByOrgID(entity);

		}

		public void DeleteByOrgID (System.Int32 orgID)
		{
		
            organization.DeleteByOrgID(orgID);

		}
		 
        public OrganizationModel GetByOrgID (System.Int32 orgID)
        {
        
			EOrganization entity = organization.GetByOrgID(orgID);
			
            OrganizationModel model = ConvertEntityToModel(entity);


            return model;
        }
        
		public OrganizationModel ConvertEntityToModel(EOrganization entity)
		{
			if (entity == null)
            {
                return null;
            }
			OrganizationModel model = new OrganizationModel();
                        model.OrgID = entity.OrgID;
                        model.OrgName = entity.OrgName;
                        model.OrgType = entity.OrgType;
                        model.ParentID = entity.ParentID;
                        model.IDPath = entity.IDPath;
                        model.NamePath = entity.NamePath;
                        model.LeaderID = entity.LeaderID;
                        model.LeaderName = entity.LeaderName;
                        model.OfficePhone = entity.OfficePhone;
                        model.Address = entity.Address;
                        model.DeleteFlag = entity.DeleteFlag;
                        model.DeleteTime = entity.DeleteTime;
                        model.DeleteUser = entity.DeleteUser;
                        model.CreateTime = entity.CreateTime;
                        model.CreateUser = entity.CreateUser;
                        model.UpdateTime = entity.UpdateTime;
                        model.UpdateUser = entity.UpdateUser;
                        return model;
		}
		
		public EOrganization ConvertModelToEntity(OrganizationModel model)
		{
			if (model == null)
            {
                return null;
            }
			EOrganization entity = new EOrganization();
                        entity.OrgID = model.OrgID;
                        entity.OrgName = model.OrgName;
                        entity.OrgType = model.OrgType;
                        entity.ParentID = model.ParentID;
                        entity.IDPath = model.IDPath;
                        entity.NamePath = model.NamePath;
                        entity.LeaderID = model.LeaderID;
                        entity.LeaderName = model.LeaderName;
                        entity.OfficePhone = model.OfficePhone;
                        entity.Address = model.Address;
                        entity.DeleteFlag = model.DeleteFlag;
                        entity.DeleteTime = model.DeleteTime;
                        entity.DeleteUser = model.DeleteUser;
                        entity.CreateTime = model.CreateTime;
                        entity.CreateUser = model.CreateUser;
                        entity.UpdateTime = model.UpdateTime;
                        entity.UpdateUser = model.UpdateUser;
                        return entity;
		}
		
		#endregion

        public IList<OrganizationModel> GetAllNoDelete()
        {
            IList<EOrganization> entityList = organization.GetAllNoDelete();

            if (entityList == null)
                return null;

            IList<OrganizationModel> modelList = new List<OrganizationModel>();

            foreach (var item in entityList)
            {
                OrganizationModel model = ConvertEntityToModel(item);
                modelList.Add(model);

            }

            return modelList;
        }

        public IList<OrganizationModel> GetNoDeleteByParentID(int parentID)
        {
            IList<EOrganization> entityList = organization.GetNoDeleteByParentID(parentID);

            if (entityList == null)
                return null;

            IList<OrganizationModel> modelList = new List<OrganizationModel>();

            foreach (var item in entityList)
            {
                OrganizationModel model = ConvertEntityToModel(item);
                modelList.Add(model);

            }

            return modelList;
        }

        public IList<OrganizationModel> GetPath()
        {
            IList<OrganizationModel> modelList = new List<OrganizationModel>();

            IList<OrganizationModel> sourceList = GetAllNoDelete();

            GetNextPath(modelList, sourceList, 0);
           
            return modelList;
        }

        private void GetNextPath(IList<OrganizationModel> modelList, IList<OrganizationModel> sourceList, int orgID)
        {

            //for (int i = 0; i < modelList.Count; i++)
            //{
            //    string ss = new string('-', modelList[i].IDPath.Length-1);
            //    ss = ss + "├";
            //    string s = modelList[i].IDPath.Replace('-', '|');
            //    modelList[i].OrgName = s + modelList[i].OrgName;
            //}


            var list = (from p in sourceList where p.ParentID == orgID select p).ToList();

            for (int i = 0; i < list.Count; i++)
            {
                int s = list[i].IDPath.Split('-').Length;

                if (s == 1)
                {
                    list[i].OrgName = "┌" + list[i].OrgName;
                }
                else
                {
                   string ss = new string('│',s-1);

                   list[i].OrgName = ss + "├" + list[i].OrgName;

                }
                modelList.Add(list[i]);

                GetNextPath(modelList, sourceList, list[i].OrgID);
            }
        }

         
    }
}